package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.POJO.CheckItem;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemDao checkItemDao;


    public PageResult findpage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        // System.out.println("i am here...........");
        PageHelper.startPage(currentPage, pageSize);

        //System.out.println("i am here2...........");
        Page<CheckItem> result = checkItemDao.findpage(queryString);

        // System.out.println("i am here3...........");
        return new PageResult(result.getTotal(), result.getResult());
    }

    @Override
    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
    }

    @Override
    public CheckItem findById(Integer id) {
        return checkItemDao.findById(id);
    }

    @Override
    public void edit(CheckItem checkItem) {
        checkItemDao.edit(checkItem);
    }

    @Override
    public void delete(Integer id) {
        //首先判断该检查项是否与检查组有关联
        long count = checkItemDao.findCountByCheckItemId(id);
        if (count > 0) {
            //已被关联，不允许删除
            throw new RuntimeException("当前检查项被引用，不能删");
        }else{
            checkItemDao.delete(id);
        }

    }
}
